package org.eae.eldf.dao.module.impl;

import java.util.ArrayList;
import java.util.List;
import org.eae.eldf.dao.module.IModuleMenuDao;
import org.eae.eldf.dto.module.ModuleMenuDto;
import org.eae.eldf.dto.module.ModuleRolePurviewDto;
import org.eae.eldf.entity.module.ModuleInfo;
import org.eae.eldf.entity.module.ModuleMenu;
import org.eae.eldf.framework.dao.JdbcDaoImpl;
import org.eae.eldf.framework.pager.Pagination;
import org.springframework.stereotype.Repository;
@Repository
public class ModuleMenuDaoImpl extends JdbcDaoImpl implements IModuleMenuDao{

	@Override
	public Pagination<ModuleMenu> pageModuleMenuList(Pagination<ModuleMenu> pager,
			String searchValue) {
		String sql = "SELECT * FROM MODULE_Menu M WHERE M.MODULE_MENU_ID ='0'";
		List<Object> params = new ArrayList<Object>();
		
			sql += " ORDER BY MODULE_INFO_ID ASC";
		
		return this.queryListByPage(pager, sql.toString(), params,
				ModuleMenu.class);
	}

	
	@Override
	public List<ModuleInfo> queryModuleMenuList() {
		String sql = "SELECT * FROM MODULE_INFO M WHERE M.MODULE_IS_DELETE ='0'";
		return this.queryBySQL(sql, ModuleInfo.class);
	}


	@Override
	public List<ModuleMenu> queryModuleMenuListById(long moduleInfoId) {
		String sql = "SELECT * FROM MODULE_MENU M WHERE  M.MODULE_INFO_ID = ?";
		return this.queryBySQL(sql, new Object[]{moduleInfoId}, ModuleMenu.class);
	}


	@Override
	public List<ModuleMenu> queryModuleMenuListByParentId(long parentId) {
		String sql = "SELECT * FROM MODULE_MENU M WHERE  M.PARENT_ID = ?";
		return this.queryBySQL(sql, new Object[]{parentId}, ModuleMenu.class);
	}


	@Override
	//查询模块角色相关 菜单 并转成菜单树DTO
	public List<ModuleRolePurviewDto> querryZtreeMenuList(Long moduleInfoId) {
		String sql = "select a.module_menu_id as pid,a.menu_name as name,a.parent_id as parentid,false as isChecked from module_menu a where 1=1 ";
		List<Object> params = new ArrayList<Object>();
		if(moduleInfoId!=null){
		sql +="and a.module_info_id=?  ";
		params.add(moduleInfoId);
		}
		sql += " ORDER BY a.module_menu_id asc";
			
		return this.queryBySQL(sql, params, ModuleRolePurviewDto.class);
	}


	@Override
	public Pagination<ModuleMenuDto> pageMenuList(Pagination<ModuleMenuDto> pager,
			Long moduleInfoId ,Boolean isMenu) {
		StringBuffer sql = new StringBuffer("select *,json_extract(i18nName,'$.zh_CN') as i18nNameCN,json_extract(i18nName,'$.en_US') as i18nNameUS from module_menu m ");
		List<Object> params = new ArrayList<Object>();
		if (isMenu == false) {
			sql.append("where m.parent_id = 0 and m.module_info_id = ?");
			params.add( moduleInfoId);
		}
		else {
			
			sql.append("where m.parent_id = ?");
			params.add( moduleInfoId);
		}
			
		sql.append(" order by module_menu_id desc");
		
		return this.queryListByPage(pager, sql.toString(), params,
				ModuleMenuDto.class);
	}

	
}
